package com.lili.math;

/**
 * @Auther: 李 力
 * @Date: 2024/8/5
 * @Description: 第 N 位数字
 * @version: 1.0
 */
public class LeetCode400 {
    /*
     * 时间复杂度: O(log10n)
     * 空间复杂度: O(1)
     * */
    public static int findNthDigit(int n) {
        int d = 1;
        int count = 9;
        while (n > (long) d * count) {
            n = n - d * count;
            d++;
            count = count * 10;
        }
        int index = n - 1;
        int start = (int) Math.pow(10, d - 1);
        int num = start + index / d;
        int diIndex = index % d;
        int di = (num / (int) (Math.pow(10, d - diIndex - 1))) % 10;
        return di;
    }
}
